package com.example.steveaoblogdemo.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;

public class ImgUpload {
    public Result<?> uploadImg(MultipartFile file){
       String originalFilename = file.getOriginalFilename();
       String fileName = System.currentTimeMillis()+"."+originalFilename.substring(originalFilename.lastIndexOf(".")+1);
       String filePath = "E:\\blog-test\\新建文件夹\\steveao-blog-vue-demo\\src\\assets\\img\\";
        File dir = new File(filePath + fileName);
        if (!dir.getParentFile().exists()){
            dir.getParentFile().mkdirs();
        }
        System.out.println("图片上传的保存位置:"+filePath+fileName);
        try{
            file.transferTo(dir);
            System.out.println("图片上传,访问url"+fileName);
            return new Result<>(200,"上传成功",fileName);
        }catch (Exception e){
            return new Result<>(400,"上传失败",null);
        }

    }
}
